﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Printing_interface
{
    [Serializable]
    public class Printing_editingBas : DataGridView
    {
		//是否在编辑中
		public bool isEdit = false;
		//事件
		public delegate void add_click(object sender);
		public event add_click _add_click;

		public delegate void delete_click(object sender);
		public event delete_click _delete_click;

		private ContextMenuStrip CMenuStrip;

		private int MROW = -1;
		public Printing_editingBas()
        {
			SetStyle(ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
			UpdateStyles();

			BindMenu();
			ContextMenuStrip = CMenuStrip;

			//优化dataGridView控件闪耀现象
			Type dgvType = base.GetType();
			PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
			pi.SetValue(this, true, null);

			//禁止编辑后自动生成一行
			base.AllowUserToAddRows = false;
			base.BackgroundColor = Color.White;
			base.BorderStyle = BorderStyle.None;
			base.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
			base.EnableHeadersVisualStyles = false;
			base.GridColor = SystemColors.ActiveCaption;
			base.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
			base.TopLeftHeaderCell.Value = "序号";
			base.RowTemplate.Height = 23;
			base.ScrollBars = ScrollBars.Both;
			base.DefaultCellStyle.Font = new Font("微软雅黑", 10.5f, FontStyle.Regular, GraphicsUnit.Point, 134);
			base.RowHeadersWidth = 60;
			DataGridViewCellStyle dataGridViewCellStyle = new DataGridViewCellStyle();
			DataGridViewCellStyle dataGridViewCellStyle2 = new DataGridViewCellStyle();
			//列标题
			dataGridViewCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;//设置表头文本居中
			dataGridViewCellStyle.BackColor = Color.FromArgb(245, 245, 255);
			dataGridViewCellStyle.Font = new Font("微软雅黑", 10.5f, FontStyle.Regular, GraphicsUnit.Point, 134);
			dataGridViewCellStyle.ForeColor = SystemColors.WindowText;
			dataGridViewCellStyle.SelectionBackColor = SystemColors.Highlight;
			dataGridViewCellStyle.SelectionForeColor = SystemColors.HighlightText;
			dataGridViewCellStyle.WrapMode = DataGridViewTriState.True;
			base.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle;

			//行标题
			dataGridViewCellStyle2.Alignment = DataGridViewContentAlignment.MiddleLeft;
			dataGridViewCellStyle2.BackColor = Color.FromArgb(245, 245, 255);
			dataGridViewCellStyle2.Font = new Font("微软雅黑", 10.5f, FontStyle.Regular, GraphicsUnit.Point, 134);
			dataGridViewCellStyle2.ForeColor = SystemColors.WindowText;
			dataGridViewCellStyle2.SelectionBackColor = SystemColors.Highlight;
			dataGridViewCellStyle2.SelectionForeColor = SystemColors.HighlightText;
			dataGridViewCellStyle2.WrapMode = DataGridViewTriState.True;
			base.RowHeadersDefaultCellStyle = dataGridViewCellStyle2;

			//单元格样式
			base.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;           //设置表格文本居中

			//禁止用户改变列宽行高
			base.AllowUserToOrderColumns = false;
			base.AllowUserToResizeRows = false;
			base.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
			base.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;

		}

		protected override void OnCellMouseDown(DataGridViewCellMouseEventArgs e)
		{
			if (e.RowIndex > -1 && e.ColumnIndex > -1 && e.Button == MouseButtons.Right && !base.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected)
			{
				base.CurrentCell = base.Rows[e.RowIndex].Cells[e.ColumnIndex];
				base.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;
			}
			base.OnCellMouseDown(e);
		}
		protected override void OnCellEndEdit(DataGridViewCellEventArgs e)
		{
			base.OnCellEndEdit(e);
			isEdit = false;
		}
		protected override void OnCellBeginEdit(DataGridViewCellCancelEventArgs e)
		{
			base.OnCellBeginEdit(e);
 
		}
        protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
        {
            base.OnEditingControlShowing(e);
        }
        protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
		{
			try
			{
				SizeF sizeF = e.Graphics.MeasureString("1", base.DefaultCellStyle.Font);
				SolidBrush brush = new SolidBrush(base.RowHeadersDefaultCellStyle.ForeColor);
				e.Graphics.DrawString((e.RowIndex + 1).ToString(CultureInfo.CurrentUICulture), base.DefaultCellStyle.Font, brush, e.RowBounds.Location.X + 15, (float)e.RowBounds.Location.Y + ((float)e.RowBounds.Height - sizeF.Height) / 2f);

				//禁止排序
				if (base.Columns.Count > 0)
				{
					for (int i = 0; i < base.Columns.Count; i++)
					{
						base.Columns[i].SortMode = DataGridViewColumnSortMode.Programmatic;
					}
				}
			}
			catch (Exception)
			{
			}
			base.OnRowPostPaint(e);
		}

		////////////////////////////////////////////////////////////////////////////////

		private void BindMenu()
		{
			CMenuStrip = new ContextMenuStrip();

			ToolStripItem toolStripItem5 = new ToolStripMenuItem();
			toolStripItem5.Name = "添加ToolStripMenuItem";
			toolStripItem5.Text = "添加行 ";
			toolStripItem5.Click += 添加ToolStripMenuItem_Click;
			CMenuStrip.Items.Add(toolStripItem5);

			ToolStripItem toolStripItem3 = new ToolStripMenuItem();
			toolStripItem3.Name = "删除ToolStripMenuItem";
			toolStripItem3.Text = "删除 (将需要删除的行，整行选中才可以)";
			toolStripItem3.Click += 删除ToolStripMenuItem_Click;
			CMenuStrip.Items.Add(toolStripItem3);

		}

		private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			try
			{
				if (!isEdit)

					for (int i = 0; i < 1; i++)
					{
						DataGridViewRowCollection rows = base.Rows;
						object[] values = new string[base.ColumnCount];
						rows.Add(values);
					}
				isEdit = true;
				this._add_click?.Invoke(null);
			}
			catch (Exception)
			{
			}
		}
		private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
			try
			{
				string[] buf = new string[base.SelectedRows.Count];
				string str_buf = "";
				for (int d = base.SelectedRows.Count - 1; d > -1; d--)
				{
					buf[d] = base.SelectedRows[d].Cells[0].FormattedValue.ToString();
					str_buf += base.SelectedRows[d].Cells[0].FormattedValue.ToString();
				}

				if (MessageBox.Show("删除当前打印名称：" + str_buf, "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
				{
					for (int num = base.SelectedRows.Count - 1; num > -1; num--)
					{
						buf[num] = base.SelectedRows[num].Cells[0].FormattedValue.ToString();
						base.Rows.Remove(base.SelectedRows[num]);
					}
					this._delete_click?.Invoke(buf);
				}
			}
			catch (Exception)
			{
			}
		}

		

    }
}
